package XMLCreator;

import java.util.*;

import javax.xml.stream.*;

import DataObjects.*;
import Main.*;

public class XMLCreatorArticle extends XMLCreator<Article>{

	protected void buildXMLInner(Article item)	throws XMLStreamException {
		startNode("aritcle");
		
		createDetail("id",item.getId()+"");
		createDetail("title",item.getTitle()+"");
		Collection<Pair<String,Object>> detailIterator=item.getMetadata();//TODO metadata
		for(Pair<String,Object> detail : detailIterator){
			createDetail(detail.getFirst(),detail.getSecend());
		}
		createDetail("topicsID",allTopicID(item.getTopics()));
		createDetail("topicsName",allTopicName(item.getTopics()));
		createDetail("text",item.getText());
		
		finishNode("aritcle");
	}

	private String allTopicID(Collection<Topic> topics){
		String ans="";
		boolean doOneLoop=false;
		for (Topic topic : topics){
			ans+=topic.getId()+",";
			doOneLoop=true;
		}
		if (doOneLoop)
			return ans.substring(0,ans.length()-1);
		else
			return "";
	}
	
	private String allTopicName(Collection<Topic> topics){
		String ans="";
		boolean doOneLoop=false;
		for (Topic topic : topics){
			ans+=topic.getName()+",";
			doOneLoop=true;
		}
		if (doOneLoop)
			return ans.substring(0,ans.length()-1);
		else
			return "";
	}
}
